package com.fastwire.carriere.inventory.entity;

import java.util.List;
import java.util.Set;

import org.tmforum.mtop.fmw.xsd.nam.v1.RelativeDistinguishNameType;
import org.tmforum.mtop.nrb.xsd.lp.v1.LayeredParametersType;
import org.tmforum.mtop.nrf.xsd.tpdata.v1.IPAddressListType;
import org.tmforum.mtop.nrf.xsd.tpdata.v1.IPAddressType;

import com.fastwire.carriere.common.mapper.TPNameMapper;
import com.fastwire.carriere.common.util.EntityUtil;
import com.fastwire.carriere.common.util.StringUtil;
import com.google.common.collect.Sets;

// TODO: Auto-generated Javadoc
/**
 * The Class TerminationPoint.
 */
public class TerminationPoint extends NetworkEntity {

	/** The slot. */
	private String slot;

	/** The ne name. */
	private String neName;

	/** The port. */
	private String port;

	/** The sub slot. */
	private String subSlot;

	/** The ip address. */
	private TPIPAddress ipAddress;

	/** The direction. */
	private String direction;

	/** The tp type. */
	private String tpType;

	/** The trans parameters. */
	private Set<TransmissionParameter> transParameters;

	/**
	 * Gets the direction.
	 * 
	 * @return the direction
	 */
	public String getDirection() {
		return direction;
	}

	/**
	 * Sets the direction.
	 * 
	 * @param direction
	 *            the new direction
	 */
	public void setDirection(String direction) {
		this.direction = direction;
	}

	/**
	 * Gets the tp type.
	 * 
	 * @return the tp type
	 */
	public String getTpType() {
		return tpType;
	}

	/**
	 * Sets the tp type.
	 * 
	 * @param tpType
	 *            the new tp type
	 */
	public void setTpType(String tpType) {
		this.tpType = tpType;
	}

	/**
	 * Instantiates a new termination point.
	 */
	public TerminationPoint() {
		super();
	}

	/**
	 * Gets the trans parameters.
	 * 
	 * @return the trans parameters
	 */
	public Set<TransmissionParameter> getTransParameters() {
		return transParameters;
	}

	/**
	 * Sets the trans parameters.
	 * 
	 * @param transParameters
	 *            the new trans parameters
	 */
	public void setTransParameters(Set<TransmissionParameter> transParameters) {
		this.transParameters = transParameters;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.fastwire.carriere.inventory.entity.NetworkEntity#setName(java.lang
	 * .String)
	 */
	@Override
	public void setName(String name) {
		super.setName(name);
		TPNameMapper.parseU2000PTPName(this);
	}

	/**
	 * Sets the ems native name.
	 * 
	 * @param aliseRefList
	 *            the alise ref list
	 * @param isAlias
	 *            the is alias
	 */
	public void setEmsNativeName(String aliseRefList, boolean isAlias) {

		if (!StringUtil.isEmptyString(aliseRefList)) {
			String[] emsNameListStrings = aliseRefList.split("::");

			String[] emsNames = emsNameListStrings[0].split("=");

			if (emsNames.length > 1) {
				this.emsNativeName = emsNames[1];
			}
		}

	}

	/**
	 * Gets the slot.
	 * 
	 * @return the slot
	 */
	public String getSlot() {
		return slot;
	}

	/**
	 * Sets the slot.
	 * 
	 * @param slot
	 *            the new slot
	 */
	public void setSlot(String slot) {
		this.slot = slot;
	}

	/**
	 * Gets the ne name.
	 * 
	 * @return the ne name
	 */
	public String getNeName() {
		return neName;
	}

	/**
	 * Sets the ne name.
	 * 
	 * @param neName
	 *            the new ne name
	 */
	public void setNeName(String neName) {
		this.neName = neName;
	}

	/**
	 * Gets the port.
	 * 
	 * @return the port
	 */
	public String getPort() {
		return port;
	}

	/**
	 * Sets the port.
	 * 
	 * @param port
	 *            the new port
	 */
	public void setPort(String port) {
		this.port = port;
	}

	/**
	 * Gets the ip address.
	 * 
	 * @return the ip address
	 */
	public TPIPAddress getIpAddress() {
		return ipAddress;
	}

	/**
	 * Sets the ip address.
	 * 
	 * @param ipAddress
	 *            the new ip address
	 */
	public void setIpAddress(TPIPAddress ipAddress) {
		this.ipAddress = ipAddress;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.fastwire.carriere.inventory.entity.NetworkEntity#setName(java.util
	 * .List)
	 */
	@Override
	public void setName(List<RelativeDistinguishNameType> rdnList) {

		this.name = EntityUtil.convertRtnListToString(rdnList);
		TPNameMapper.parseU2000PTPName(this);
	}

	/**
	 * Gets the sub slot.
	 * 
	 * @return the sub slot
	 */
	public String getSubSlot() {
		return subSlot;
	}

	/**
	 * Sets the sub slot.
	 * 
	 * @param subSlot
	 *            the new sub slot
	 */
	public void setSubSlot(String subSlot) {
		this.subSlot = subSlot;
	}

	/**
	 * Sets the trans parameters.
	 * 
	 * @param LayeredtransParameters
	 *            the layeredtrans parameters
	 * @param isEnabled
	 *            the is enabled
	 */
	public void setTransParameters(
			List<LayeredParametersType> LayeredtransParameters,
			boolean isEnabled) {
		if (LayeredtransParameters == null) {
			return;
		}

		Set<TransmissionParameter> transmissionParameters = Sets.newHashSet();
		for (LayeredParametersType layeredParametersType : LayeredtransParameters) {
			TransmissionParameter transmissionParameter = new TransmissionParameter(
					layeredParametersType);
			transmissionParameters.add(transmissionParameter);
		}
		setTransParameters(transmissionParameters);

	}

	/**
	 * Sets the ip address.
	 * 
	 * @param ipAddressListType
	 *            the new ip address
	 */
	public void setIpAddress(IPAddressListType ipAddressListType) {

		if (ipAddressListType == null) {
			return;
		}
		List<IPAddressType> ipAddressTypes = ipAddressListType.getIPAddress();

		if (!ipAddressTypes.isEmpty()) {

			IPAddressType ipAddressType = ipAddressTypes.get(0);
			ipAddress = new TPIPAddress();

			ipAddress.setAddress(ipAddressType.getAddress());
			ipAddress.setMask(ipAddressType.getMask());
			ipAddress.setType(ipAddressType.getType());

		}

	}

}
